/***************************************************************************
 *   Copyright (C) 2009 by maarten,,,   *
 *   maarten@maarten-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "mainscreen.h"

// qt includes
#include <QApplication>
#include <QFont>
#include <QLCDNumber>
#include <QPushButton>
#include <QSlider>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QTextEdit>
#include <QWidget>
#include <QFrame>
#include <QPainter>
#include <QLabel>
#include <QLineEdit>




// other includes
#include "parser/parser.h"
#include "scenemanager/scenemanager.h"
#include "objects/objectlist.h"
#include "cgpanel/cgpanel.h"

#include <iostream>
using namespace std;

// test matrix
#include "utils/matrix.h"


MainScreen::MainScreen(QWidget *parent)
 : QWidget(parent)
{
    // setup menu
    createMenu();

    //create draw panel
    map = new draw::CgPanel(parent);
    mainLayout.addWidget(map);
   
    // finish
    setLayout(&mainLayout);
    showFullScreen();
}

MainScreen::~MainScreen()
{
    cout << "\n\nCleaning up...\n";
    cout << "Deleting cgpanel...\n";
    if(map!=NULL)
      delete map;
    
    cout << "Deleting menu...\n"; 
    if(exitAction != NULL)
      delete exitAction;
    if(openFile != NULL)
      delete openFile;
    if(saveImage != NULL)
      delete saveImage;
    if(clearScr != NULL)
      delete clearScr;
    if(drawScene != NULL)
      delete drawScene;
    if(fileMenu != NULL)
      delete fileMenu;
    if(menu != NULL)
      delete menu;
}

void MainScreen::createMenu()
{
    menu =  new QMenuBar;

    fileMenu = new QMenu(tr("&File"), this);
    exitAction = fileMenu->addAction(tr("E&xit"));
    openFile= fileMenu->addAction(tr("O&pen scene..."));
    clearScr= fileMenu->addAction(tr("C&lear..."));
    drawScene= fileMenu->addAction(tr("D&raw scene!"));
    saveImage = fileMenu->addAction(tr("S&ave image..."));
    
    menu->addMenu(fileMenu);

    connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(openFile, SIGNAL(triggered()), this, SLOT(loadScene()));
    connect(clearScr, SIGNAL(triggered()), this, SLOT(cleanScreen()));
    connect(drawScene, SIGNAL(triggered()), this, SLOT(startToDraw()));
    connect(saveImage, SIGNAL(triggered()), this, SLOT(saveToImage()));

    mainLayout.setMenuBar(menu);
}
void MainScreen::loadScene()
{
    map->readSceneFile();
}


void MainScreen::saveToImage()
{
    map->saveImage();
}   

void MainScreen::cleanScreen()
{
    map->cleanScreen();
}

void MainScreen::startToDraw()
{
    map->startRaytracing();
}